package com.gklyj.controller.qiuzhi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gklyj.pojo.Feedback;
import com.gklyj.pojo.Position;
import com.gklyj.pojo.User;
import com.gklyj.service.feedbackService;
import com.gklyj.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

@Controller
@Transactional
public class Qz_feedbackController {

    @Autowired
    userService userService;

    @Autowired
    feedbackService feedbackService;
    /*
    上传图片，将图片上传到feedbackPictures目录
     */
    @PostMapping("/uploadFeedbackPicture")
    @ResponseBody
    public JSON uploadFile(MultipartFile file, HttpServletRequest request) {
        JSONObject json = new JSONObject();
        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/feedbackPictures/";//上传到这个文件夹
        File file1 = new File(filePath);
        //如果没有的话创建一个
        if (!file1.exists()) {
            file1.mkdirs();
        }

        //路径+文件名
        //文件名：file.getOriginalFilename()
        String finalFilePath = filePath + file.getOriginalFilename().trim();
        File desFile = new File(finalFilePath);
        if (desFile.exists()) {
            desFile.delete();
        }
        try {
            file.transferTo(desFile);
            json.put("code", 0);
            //将文件名放在msg中，前台提交表单时需要用到
            json.put("msg", file.getOriginalFilename().trim());

            JSONObject json2 = new JSONObject();
            json2.put("src", finalFilePath);
//            json2.put("title", "");
//            json.put("Data", json2);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            json.put("code", 1);
        }
        System.out.println(json);
        return json;
    }

    /*
    提交反馈信息
     */
    @PostMapping("/addFeedback")
    @ResponseBody
    public boolean addFeedback(@RequestBody Feedback feedback, HttpServletRequest request) throws RuntimeException{
        System.out.println(feedback);

        //添加时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String str= df.format(now);
        feedback.setTime(str);

        //添加当前用户名
        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        feedback.setName(user.getName());
        feedback.setU_id(user.getU_id());

        feedback.setState("未查看");
        return feedbackService.save(feedback);
    }

    @GetMapping("/toFeedbackDetail")
    public String toFeedbackDetail(@RequestParam("id") String id, Model model){
        Feedback feedback = feedbackService.getById(Integer.valueOf(id));
        model.addAttribute("detail",feedback);
        return "zhaoping/zp-feedback-detail";
    }
}
